package com.example.common.ui;

import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.common.R;
import com.example.common.widget.state.NetworkStateLayout;
import com.example.common.widget.state.NetworkStateView;
import com.example.common.widget.state.OnAnewRequestNetworkListener;

public abstract class AbsNetworkStateActivity extends BaseActivity implements
        OnAnewRequestNetworkListener, NetworkStateView {

    private NetworkStateLayout mStateLayout;

    @Override
    public void setContentView(int layoutResID) {
        LinearLayout rootView = (LinearLayout) View.inflate(this, R.layout.activity_network_state, null);

        // 添加标题
        int titleLayoutRes = getTitleLayoutRes();
        if (titleLayoutRes != 0){
            rootView.addView(LayoutInflater.from(this).inflate(titleLayoutRes, rootView, false), 0);
        }

        // 添加状态布局
        mStateLayout = rootView.findViewById(R.id.mNetworkStateLayout);
        mStateLayout.addView(LayoutInflater.from(this).inflate(layoutResID, mStateLayout, false));
        super.setContentView(rootView);

        // 重新请求网络监听
        mStateLayout.setOnAnewRequestNetworkListener(this);
    }

    @Override
    public void notNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.NOT_NETWORK_STATE);
    }

    @Override
    public void loadingNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.LOADING_NETWORK_STATE);
    }

    @Override
    public void errorNetwork(CharSequence charSequence) {
        mStateLayout.showNetworkStateView(NetworkStateLayout.ERROR_NETWORK_STATE, charSequence);
    }

    @Override
    public void successNetwork() {
        mStateLayout.showContentView();
    }

    @Override
    public void emptyNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.EMPTY_NETWORK_STATE);
    }

    @Override
    public void onAnewRequestNetwork() {
        onAnewRequest();
    }

    /**
     * 获取标题布局
     */
    public @LayoutRes int getTitleLayoutRes() {
        return 0;
    }

    /**
     * 重新请求网络
     */
    public abstract void onAnewRequest();
}
